(function(){
    window.App = {
        init: function(){
            var init = Yun_Initialize();
            if (init == 0) {
                Yun_StartWebService();
                $("#login").removeClass("hide");
            } else {
                $("#init").removeClass("hide");
            }
        },
        logout: function(){
            Yun_Logout();
            location.reload();
        },
        login: function(){
            var username = $("#username").val();
            var server = $("#server").val();
            var valid_username = username != "";
            var valid_server = this.is_server(server);
            !valid_username && toastr.error("用户名为空！");
            !valid_server && toastr.error("无效服务器地址！");
            valid_username && valid_server && (function(){
                App.Server = server;
                App.NickName = username;
                Yun_Login(server, username, username);
            })();
        },
        switch_camera: function(e){
            if (App.VideoStatus) {
                Yun_CloseCamera(App.CameraIndex);
                App.CameraIndex = parseInt(e.value) 
                Yun_OpenCamera(App.CameraIndex, 0);
            } else {
                App.CameraIndex = parseInt(e.value) 
            }
        },
        switch_resolution: function(e){
            if (e.value) {
                var res = e.value.split("x");
                Yun_SetVideoResolution(0, res[0], res[1]);
                Yun_SetVideoApplyParam(0);
            }
        },
        enter_room: function(){
            var roomid = $("#roomid").val();
            var valid_roomid = roomid != "";
            if (valid_roomid) {
                Yun_JoinConference(App.RoomId, "123456", "110");
            } else {
                toastr.error("会议号为空！");
            }
        },
        is_server: function(server){
            var ip_port = [];
            try {
                ip_port = server.split(":");
            } catch (e) {
                
            }
            if (ip_port.length != 2) return false;
            if (is.not.ipv4(ip_port[0])) return false;
            if (ip_port[1] > 65535 || ip_port[1] < 0) return false;
            return true;
        },
        video_record: function(){
            $("#record").toggleClass("on");
            if (App.RecordingStatus) {
                Yun_StopRecord(App.RecordingHandle);
                App.RecordingStatus = false;
            } else {
                App.RecordingHandle = Yun_StartRecord(1, JSON.stringify({
                    "mode":"one",
                    "format":"mp4",
                    "video":"true",
                    "audio":"true",
                    "filename":"yun_record",
                    "serverrecord":"false",
                    "overlaytext":
                        {
                            "text":"yun_Good_20151225{timestamp}",
                            "x":10,
                            "y":10
                        },
                    "layout":[
                        {
                            "index":1,
                            "usercode":"",
                            "channelindex":0
                        }
                    ]
                }));
                App.RecordingStatus = true;
            }
        },
        file_upload: function(){
            $("#video-progress,#image-progress").removeClass("hide");
            if (App.RecordFile) {
                App.UploadingVideo = Yun_SendFile2Server(App.RecordFile, -1, "1");
            }
            if (App.SnapshotFile) {
                App.UploadingImage = Yun_SendFile2Server(App.SnapshotFile, -1, "1");
            }
        },
        hangup: function(){
            Yun_CloseCamera(0);
            Yun_CloseMicrophone(0);
            document.getElementById("LocalVideo").removeChild(document.getElementById("LocalObject"));
            Yun_ExitConference(App.RoomId);
            var video = '<li class="video-placeholder">' +
                '<div class="item-video">' +
                '</div>' +
            '</li>';
            $("#record-video-list").html(video);
            var snapshot = '<li class="snapshot-placeholder">' +
                '<div class="item-snapshot">' +
                '</div>' +
            '</li>';
            $("#snapcapture-list").html(snapshot);
        },
        snap_capture: function(){
            Yun_Snapshot(1, JSON.stringify({
                "usecode" : "",
                "channelindex" : 0,
                "filename":	""
            }));
        },
        append_video: function(url){
            var video = '<li>' +
                '<div class="item-video">' +
                    '<video controls src="' + url + '"></video>' +
                '</div>' +
            '</li>';
            $("#record-video-list").html(video);
        },
        append_snapshot: function(url){
            var snapshot = '<li>' +
                '<div class="item-snapshot">' +
                    '<img src="' + url + '"/>' +
                '</div>' +
            '</li>';
            $("#snapcapture-list").html(snapshot);
        },
        switch_audio: function(){
            $("#switch-audio").toggleClass("on");
            if (App.AudioStatus) {
                Yun_CloseMicrophone(0);
                App.AudioStatus = false;
            } else {
                Yun_OpenMicrophone(0, 0);
                App.AudioStatus = true;
            }
        },
        switch_video: function(){
            $("#switch-video").toggleClass("on");
            if (App.VideoStatus) {
                Yun_CloseCamera(0);
                App.VideoStatus = false;
            } else {
                Yun_OpenCamera(0, 0);
                App.VideoStatus = true;
            }
        }
    };
    window.onbeforeunload = function(){
        Yun_Logout();
    }
    App.init();
})();